
package harlequin;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;


public class ShowAdd extends AddEditDeleteShow{


    private String addquery = "insert into play(title, dateTimeslot, price, slot,  audienceCount"
                      + ") values(?,?,?,?,?)";

    public ShowAdd(Connection connector) {
        
        this.connector = connector;
        this.setLocationByPlatform(rootPaneCheckingEnabled);
        this.setTitle("Add Show");
        this.setSize(320, 320);
        initComponents();
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tPrice = new javax.swing.JTextField();
        tSlot = new javax.swing.JTextField();
        bAddShow = new javax.swing.JButton();
        bBack = new javax.swing.JButton();
        tTitle = new javax.swing.JTextField();
        tDateTimeslot = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Title:");

        jLabel2.setText("Date-Timeslot:");

        jLabel3.setText("Price:");

        jLabel4.setText("Slot:");

        bAddShow.setText("Add Show");
        bAddShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddShowActionPerformed(evt);
            }
        });

        bBack.setText("Back");
        bBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addComponent(tTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tDateTimeslot, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE))
                            .addComponent(jLabel3)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(63, 63, 63)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tPrice, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                                    .addComponent(tSlot, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bBack, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(bAddShow, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tDateTimeslot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tSlot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(bBack))
                    .addComponent(bAddShow))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void bAddShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddShowActionPerformed
 dateTimeslot = tDateTimeslot.getText();  
    try {
            title = tTitle.getText();
            dateTimeslot = tDateTimeslot.getText();
            price = tPrice.getText();
            slot = tSlot.getText();            
            
            if(title.isEmpty() && dateTimeslot.isEmpty() && price.isEmpty() && slot.isEmpty() )
             JOptionPane.showMessageDialog(null, "Adding failed! \nPlease input all empty fields.");
            else if(title.isEmpty())
             JOptionPane.showMessageDialog(null, "Adding failed! \nPlease input the Title");
            else if(dateTimeslot.isEmpty())
             JOptionPane.showMessageDialog(null, "Adding failed! \nPlease input the Date");
            else if (price.isEmpty())
                JOptionPane.showMessageDialog(null, "Adding failed!\n Please input the Price");
            else if(slot.isEmpty())
             JOptionPane.showMessageDialog(null, "Adding failed!\n Please input number of Slots");
            else if (DateExists(dateTimeslot))
                JOptionPane.showMessageDialog(null, "Adding failed! \nDate already exsists");
            else if (dateTimeslot.length() < 18)
                JOptionPane.showMessageDialog(null, "Adding failed! \nDate format should be: mm/dd/yy");
            else    
            {
            
        java.sql.PreparedStatement preparedStatement = connector.prepareStatement(addquery);
            preparedStatement.setString(1, title);
            preparedStatement.setString(2, dateTimeslot);
            preparedStatement.setString(3, price);
            preparedStatement.setString(4, slot);
            preparedStatement.setInt(5, audienceCount);
            preparedStatement.executeUpdate();
            JOptionPane.showMessageDialog(null, "Addition Success!");    
            
            this.dispose(); 
            
}
  } catch (SQLException ex) {
            Logger.getLogger(ShowAdd.class.getName()).log(Level.SEVERE, null, ex);
  }
}//GEN-LAST:event_bAddShowActionPerformed

private void bBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBackActionPerformed
   this.dispose();
}//GEN-LAST:event_bBackActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAddShow;
    private javax.swing.JButton bBack;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField tDateTimeslot;
    private javax.swing.JTextField tPrice;
    private javax.swing.JTextField tSlot;
    private javax.swing.JTextField tTitle;
    // End of variables declaration//GEN-END:variables
}
